{% extends 'usercenter-base.html' %}
{% block title %} 我的收藏{% endblock %}
{% load staticfiles %}
{% block custom_bread %}
    <section>
        <div class="wp">
            <ul class="crumbs">
                <li><a href="{% url 'index' %}">首页</a>></li>
                <li><a href="{% url 'users:user_info' %}">个人中心</a>></li>
                <li>我的收藏</li>
            </ul>
        </div>
    </section>
{% endblock %}

{% block right_content %}
    <div class="right">
        <table class="table">
            <caption>专利</caption>
            <thead>
            <tr>
                <th>名称</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {% for patent in patent_list %}
                <tr key="{{ patent.id }}" type="1">
                    <td><a href="/patent/detail/{{ patent.id }}/">{{ patent.name }}</a></td>
                    <td><span class="fav">取消收藏</span></td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
        <table class="table">
            <caption>项目</caption>
            <thead>
            <tr>
                <th>名称</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {% for project in project_list %}
                <tr key="{{ project.id }}" type="2">
                    <td><a href="/project/detail/{{ project.id }}/">{{ project.name }}</a></td>
                    <td><span class="fav">取消收藏</span></td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

{% endblock %}
{% block custom_js %}
    <script type="text/javascript">
        //收藏分享
        function add_fav(current_elem, fav_id, fav_type) {
            console.log('fav_id',fav_id,'fav_type',fav_type)
            $.ajax({
                cache: false,
                type: "POST",
                url: "{% url "operation:add_fav" %}",
                data: {'fav_id': fav_id, 'fav_type': fav_type},
                async: true,
                beforeSend: function (xhr, settings) {
                    xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                },
                success: function (data) {
                    if (data.status == 'fail') {
                        if (data.msg == '用户未登录') {
                            window.location.href = "{% url 'login' %}?next={{ request.path }}";
                        } else {
                            alert(data.msg)
                        }

                    } else if (data.status == 'success') {
                        if (data.msg === '收藏') {
                            current_elem.addClass('module-item-btn')
                            current_elem.removeClass('module-item-btn-faved')
                            current_elem.text(data.msg)
                        } else {
                            current_elem.addClass('module-item-btn-faved')
                            current_elem.removeClass('module-item-btn')
                            current_elem.text(data.msg)
                        }
                    }
                },
            });
        }

        $('.fav').on('click', function () {
            add_fav($(this), $(this).parent().parent().attr('key'), $(this).parent().parent().attr('type'));
        });


    </script>
{% endblock %}